package cn.circle.mobile.controller;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.config.annotation.EnableWebMvc;

import cn.circle.mobile.service.IUserService;
import cn.circle.system.model.OrderDeatil;
/*import cn.circle.commom.model.CustJudge;*/
import cn.circle.system.model.OrderInfo;
import cn.circle.system.model.SysUser;
import cn.circle.system.pagemodel.ReturnInfo;
import cn.circle.system.service.IOrderInfoService;
import cn.circle.system.service.ISysCustomerService;
import cn.circle.system.service.ISysUserService;

/**
 * 个人中心页面
 * 
 * @author 
 *
 */
@Controller
@EnableWebMvc
@SuppressWarnings("unchecked")
public class PersonalCenterController extends FrontBaseController{
	private static final Log log = LogFactory.getLog(MobileMainController.class);
	
	
	@Autowired
	private IOrderInfoService orderInfoService;
	
	@Autowired
	private IUserService userService;
	
	@Autowired
	private ISysUserService sysUserService;
	/**
	 * 进入个人中心页面
	 */
/*	@RequestMapping(value = "/mobile/personal")
	public String index(HttpServletRequest request,HttpServletResponse response) {
		
		HttpSession session = request.getSession();

		

		return super.index("/WEB-INF/pages/webapp/PersonalCenter/personMain.jsp", request);

	}*/
	/**
	 * 
	 * 获取我的订单
	 */
	/*@RequestMapping(value="/mobile/myOrder")
	public String myordel(Integer custId,HttpServletRequest request){
		
		List<SysCustomer> kk=sysCustomerService.getUserByOpenId(getWUserOpenId(request));
		
		List<OrderInfo> orderInfos;
		try {
			orderInfos = orderInfoService.findMyOrder(kk.get(0).getCustId());
			System.out.println(orderInfos.get(0).getPayPrice());
			request.setAttribute("orderInfo",orderInfos);
			request.setAttribute("orderId",5);
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			return super.index("/WEB-INF/pages/webapp/PersonalCenter/myorder.jsp", request);
		}
		
		return super.index("/WEB-INF/pages/webapp/PersonalCenter/myorder.jsp", request);
		
	}*/
	/**
	 * 获取我的订单
	 * @param returnInfo
	 * @return
	 */
	@ResponseBody
	@RequestMapping(value="/mobile/getMyOrder")
	public ReturnInfo getmyordel(ReturnInfo returnInfo, String token, int type){
      String phone =  userService.getUserByToken(token);
      String hql;
		if(0 == type){
			  hql = " from  OrderInfo o where o.phone = "+ phone +"  order by orderId desc ";			 
		}else{
			 hql = " from  OrderInfo o where o.phone = "+ phone +" and payStatus = "+type+" order by orderId desc  ";
			 
		}     
		List<OrderInfo> orderInfo = super.find(hql);
		returnInfo.setRows(super.count("select count(*) " + hql)); // 总记录数		
		returnInfo.setObj(orderInfo);
		return returnInfo;
	}
	/**
	 * 订单详情
	 */
	@ResponseBody
	@RequestMapping(value="/mobile/orderdeatial")
	public ReturnInfo orderdetial(ReturnInfo returnInfo, String orderNo){
		List<OrderDeatil> OrderDeatils = null;
		String hql = " from  OrderDeatil o where o.dtlOrderNo = '"+ orderNo +"'  order by dtlId desc ";
		OrderDeatils = super.find(hql);
		returnInfo.setRows(super.count("select count(*) " + hql)); // 总记录数
		returnInfo.setObj(OrderDeatils);
		return returnInfo;		
	}
	/**
	 * 个人资料  personalData
	 */
	@RequestMapping(value="/mobile/personalData")
	public String personalData(HttpServletRequest request, String token){
		
		String phone = userService.getUserByToken(token);	
		if(phone == null){
			return "浏览器异常";
		}
		String hql = " from  SysUser o where o.status = 9 and o.phone = "+phone+"";
		List<SysUser> sysUser = super.find(hql);
		HttpSession session = request.getSession();
		session.setAttribute("sysUser",sysUser.get(0));
		return super.index("/WEB-INF/pages/mobile/main/personalcenter/personalData.jsp", request);		
	}

	/**
	 * 待付款  
	 */
	@RequestMapping(value="/mobile/waitPay")
	public String waitPay(HttpServletRequest request){
		System.out.println("待付款");
		/*List<SysCustomer> kk=sysCustomerService.getUserByOpenId(getWUserOpenId(request));
		List<OrderInfo> orderInfos=orderInfoService.waitPay(kk.get(0).getCustId());
		request.setAttribute("orderInfos",orderInfos);*/
		return super.index("/WEB-INF/pages/webapp/PersonalCenter/waitPay.jsp", request);
		 
		
		
	}
	
	/**
	 * 待发货
	 */
	@RequestMapping(value="/mobile/waitCargo")
	public String waitCargo(HttpServletRequest request){
	/*	List<SysCustomer> kk=sysCustomerService.getUserByOpenId(getWUserOpenId(request));
		List<OrderInfo> orderInfos=orderInfoService.waitCargo(kk.get(0).getCustId());
		request.setAttribute("orderInfos",orderInfos);*/
		return super.index("/WEB-INF/pages/webapp/PersonalCenter/waitCargo.jsp", request);
	
	}
	/**
	 * 待收货
	 */
	@RequestMapping(value="/mobile/waitTheGoods")
	public String waitTheGoods(HttpServletRequest request){
	/*	List<SysCustomer> kk=sysCustomerService.getUserByOpenId(getWUserOpenId(request));
		
		List<OrderInfo> orderInfos=orderInfoService.waitTheGoods(kk.get(0).getCustId());
		
		request.setAttribute("orderInfos",orderInfos);*/
		return super.index("/WEB-INF/pages/webapp/PersonalCenter/waitTheGoods.jsp", request);
	
	}


	/**
	 * 确认收货
	 */
	
	@RequestMapping("/mobile/confirm")
	public String confirm(Long orderId,HttpServletRequest request){
		
		try {
			orderInfoService.confirm(orderId);
			request.setAttribute("confirm","确认收货成功");
		} catch (Exception e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			request.setAttribute("confirm","确认收货失败");
		}
		 return waitTheGoods(request);
		
		
		
	}
	
	/**
	 * 取消订单
	 */
	@RequestMapping(value = "/mobile/cancelOrder")
	public String cancelOrder(Integer orderId, Integer commodityId, HttpServletRequest request) {
		if (orderId != null && commodityId != null) {
			try {
				String hql = "update OrderInfo s set s.sendStatus=0 where s.orderId =" + orderId
						+ " and s.commodityInfo.commodityId = " + commodityId;
				super.executeHql(hql);
				request.setAttribute("result", "你已经成功取消了该订单！");
				
				//商品数量回增
				hql = "from OrderInfo s  where s.orderId =" + orderId
						+ " and s.commodityInfo.commodityId = " + commodityId;
				OrderInfo o = (OrderInfo) super.find(hql).get(0);
				hql = "update CommodityInfo s set s.number = s.number + "+o.getOrderNumber()+" where  s.commodityId = " + commodityId;
				super.executeHql(hql);
				
				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
				request.setAttribute("result", "由于网络原因，订单取消失败！");
			}
/*			List<SysCustomer> kk = sysCustomerService.getUserByOpenId(getWUserOpenId(request));
			List<OrderInfo> orderInfos = orderInfoService.waitCargo(kk.get(0).getCustId());*/
	/*		request.setAttribute("orderInfos", orderInfos);*/
		}
		return super.index("/WEB-INF/pages/webapp/PersonalCenter/waitCargo.jsp", request);
	}
	
	@ResponseBody
	@RequestMapping(value = "/mobile/savePerson")
	public ReturnInfo savePerson(ReturnInfo info, SysUser sysUser, HttpServletRequest request) {
		if (sysUser != null) {
			try {
				
				sysUserService.update(sysUser);

			} catch (Exception e) {
				info.setMsg("保存失败");
				info.setSuccess(1);
			}

		}
		return info;
	}
	
	
	
	
	
	
}
